VS Code Extension API: Decorations

您所在的位置:网站首页 decoration of VS Code Extension API: Decorations

VS Code Extension API: Decorations

2023-10-20 05:41| 来源: 网络整理| 查看: 265

What are decorations?

The Decorations API was introduced to Visual Studio Code as a way to decorate pieces of code. It allows you to grab ranges of text in an editor and decorate it using a subset of CSS properties. It does not modify the code but rather gives a visual addition in VS Code to how you can interpret your code.

As well, in VS Code 1.3, they added support to use the ::before and ::after pseudoelements on these decorations through the API which is commonly seen in various extensions now in 2018.

Extensions that use decorations

Decorations has been widely adopted across the extension community and here are a sample of extensions that show off what can be done with decorations:

extension logo GitLens — Git supercharged filled star filled star filled star filled star filled star (368) Eric Amodio 36,559,547 installs

Supercharge the Git capabilities built into Visual Studio Code — Visualize code authorship at a glance via Git blame annotations and code lens, seamlessly navigate and explore Git repositories, gain valuable insights via powerful comparison commands, and so much more

extension logo Import Cost filled star filled star filled star filled star empty star (34) Wix 1,292,877 installs

Display import/require package size in the editor

extension logo Quokka.js filled star filled star filled star filled star empty star (89) Wallaby.js 11,041,149 installs

Live Scratchpad for JavaScript.

extension logo Wolf filled star filled star filled star filled star filled star (4) traBpUkciP 6,449 installs

Python live scratchpad

extension logo TODO Highlight filled star filled star filled star filled star empty star (74) Wayou Liu 1,448,633 installs

highlight TODOs, FIXMEs, and any keywords, annotations...

extension logo colorize filled star filled star filled star filled star empty star (15) kamikillerto 459,707 installs

A vscode extension to help visualize css colors in files.

extension logo JS Parameter Annotations filled star filled star filled star filled star filled star (20) Benjamin Lannon 16,375 installs

Annotations for parameters in your JS / TS Files to mimic named parameters

extension logo Log File Highlighter filled star filled star filled star filled star filled star (23) Emil Åström 504,729 installs

Adds color highlighting to log files to make it easier to follow the flow of log events and identify problems.

: Highlights log4net logfiles to make them more readable How to create decorations

To show off how to use the decorations, I am going to be writing an example extension that will highlight all locations of console.log in JS files

If you want to download the code to follow along, head over to lannonbr/console-log-highlighter. It was generated with the yeoman vscode generator.

If you open up src/extension.ts, The first piece of code is the decoration type.

const decorationType = vscode.window.createTextEditorDecorationType({ backgroundColor: 'green', border: '2px solid white', })

This describes how we want to style the decoration. For this example, I am going to just decorate the text itself, but if you are interested, you can add before and after objects to add pseudo elements to the decoration. (GitLens uses this to push the inline blame after the end of the line)

The following things can be styled which map to their respective css properties:

border outline color backgroundColor fontWeight textDecoration letterSpacing opacity

As well, you can insert these into light and dark objects so you can customize how it will appear for specifically light or dark color themes.

Next in the activate function, I have a event listener which will watch for document saves. Any time the file is saved this will be called. I grab the editor open and pass it into a decorate function which will do the heavy lifting on setting the decorations:

vscode.workspace.onWillSaveTextDocument(event => { const openEditor = vscode.window.visibleTextEditors.filter( editor => editor.document.uri === event.document.uri )[0] decorate(openEditor) })

Following is the decorate function which takes a vscode.TextEditor as a parameter. First we setup some boilerplate by grabbing the text from the editor, setting up a regex to look for console.log, and as well create an array where the decorations will live.

let sourceCode = editor.document.getText() let regex = /(console\.log)/ let decorationsArray: vscode.DecorationOptions[] = []

Next I loop over each line of the code and will try looking for console.log. If I can find it, I create a range for which the decoration will cover. Each decoration requires at least a range which includes a starting and ending position for where the text is. In VS Code internally, line and columns are 0-indexed, so if we had a file have console.log at the beginning of the document, it would start at line 0, col 0.

const sourceCodeArr = sourceCode.split('\n') for (let line = 0; line { const openEditor = vscode.window.visibleTextEditors.filter( editor => editor.document.uri === event.document.uri )[0] decorate(openEditor) }) } function decorate(editor: vscode.TextEditor) { let sourceCode = editor.document.getText() let regex = /(console\.log)/ let decorationsArray: vscode.DecorationOptions[] = [] const sourceCodeArr = sourceCode.split('\n') for (let line = 0; line


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3